package com.xyy.saas.payment.service.impl;

import com.xyy.saas.payment.constants.Constants;
import com.xyy.saas.payment.converter.JdCreditRepayConverter;
import com.xyy.saas.payment.dao.mapper.LoanRepayPlanDetailMapper;
import com.xyy.saas.payment.dao.mapper.LoanRepayPlanMapper;
import com.xyy.saas.payment.dao.mapper.OrderTransDataMapper;
import com.xyy.saas.payment.dao.model.LoanRepayPlanDetailPo;
import com.xyy.saas.payment.dao.model.LoanRepayPlanPo;
import com.xyy.saas.payment.dao.model.OrderTransDataPo;
import com.xyy.saas.payment.dto.OrderTransDataQo;
import com.xyy.saas.payment.pojo.vo.JdCreditBIllListReq;
import com.xyy.saas.payment.pojo.vo.JdCreditBIllListVo;
import com.xyy.saas.payment.pojo.vo.JdCreditBillRepayVo;
import com.xyy.saas.payment.pojo.vo.PageVo;
import com.xyy.saas.payment.service.JdCreditRepayService;
import com.xyy.saas.payment.util.ExportExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liujf
 */
@Slf4j
@Service
public class JdCreditRepayServiceImpl implements JdCreditRepayService {

    @Resource
    private OrderTransDataMapper orderTransDataMapper;

    @Resource
    private LoanRepayPlanMapper repayPlanMapper;

    @Resource
    private LoanRepayPlanDetailMapper repayPlanDetailMapper;


    @Override
    public PageVo<JdCreditBIllListVo> billList(JdCreditBIllListReq req) {
        OrderTransDataQo transDataQo = JdCreditRepayConverter.req2qo(req);

        Integer pageSize = req.getPageSize() == null ? 10 : req.getPageSize();
        Integer offset = (((req.getPage() == null || req.getPage() <= 0) ? 1 : req.getPage()) - 1) * pageSize;
        List<OrderTransDataPo> transDataPoList = orderTransDataMapper.pageList(transDataQo, offset, pageSize);

        long count = orderTransDataMapper.count(transDataQo);
        Long pages = (count + pageSize - 1) / pageSize;
        return PageVo.pack(JdCreditRepayConverter.toBill(transDataPoList), pages, count);
    }

    @Override
    public JdCreditBillRepayVo billDetail(String payNo) {
        LoanRepayPlanPo repayPlanPo = repayPlanMapper.queryByPayNo(payNo);
        List<LoanRepayPlanDetailPo> planDetailPos = repayPlanDetailMapper.queryByPayNo(payNo);
        return JdCreditRepayConverter.repayPlan2vo(repayPlanPo, planDetailPos);
    }

    @Override
    public void billExportList(JdCreditBIllListReq req, HttpServletResponse response) throws IOException {
        OrderTransDataQo transDataQo = JdCreditRepayConverter.req2qo(req);
        long count = orderTransDataMapper.count(transDataQo);

        int pageSize = 200;
        long pages = (count + pageSize - 1) / pageSize;

        List<JdCreditBIllListVo> bIllListVos = new ArrayList<>((int) count);
        for (int page = 1; page <= pages; page++) {
            int offset = (page - 1) * pageSize;
            bIllListVos.addAll(JdCreditRepayConverter.toBill(orderTransDataMapper.pageList(transDataQo, offset, pageSize)));
        }

        String excelFileName = "京东金融还款计划.xlsx";
        String sheetName = "还款计划";
        String headers[] = Constants.JD_CREDIT_REPAY_PLAN;
        String fieldNames[] = Constants.JD_CREDIT_REPAY_PLAN_FIELD;
        ExportExcelUtil.createExcelDateFormat2(response, excelFileName, sheetName, headers, fieldNames, bIllListVos);
    }
}
